package com.ruoyi.auth.api.produce.impl;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.auth.api.produce.IOauthApiService;
import com.ruoyi.auth.constants.ValidateSubjectEnum;
import com.ruoyi.auth.verification.cache.VerificationCodeCache;
import com.ruoyi.auth.verification.service.impl.VerificationCodeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.stereotype.Service;

/**
 * @author liyang
 * @description
 * @date 2022-11-22 11:32:21
 */
@Service
public class OauthApiService implements IOauthApiService {

    @Autowired
    private TokenStore tokenStore;

    @Autowired
    private VerificationCodeServiceImpl verificationCodeService;

    @Autowired
    private VerificationCodeCache verificationCodeCache;

    @Override
    public void logOut(String token) {
        if (StrUtil.isEmpty(token)) {
            return;
        }
        OAuth2AccessToken oAuth2AccessToken = tokenStore.readAccessToken(token);
        tokenStore.removeAccessToken(oAuth2AccessToken);
    }

    @Override
    public boolean checkCaptcha(String token,String code) {
        String subject = ValidateSubjectEnum.IMAGE.getCode();
        boolean validate = this.verificationCodeService.validate(token, code, subject, true);
        return validate;
    }
}
